$j=jQuery.noConflict();
var ajaxFetchingUrl = '';

jQuery(document).ready(function(){
	ajaxFetchingUrl = blogurl+"/wp-content/plugins/facebook-comments-points-fcp/fcp_ajax_fetching.php";
	
	$j('#fcpCommentForm').submit(function(){										   		
		var isLoggedUser 			= $j('#author').length == 0 && $j('#email').length == 0;
		var fcp_comments_textarea	= encodeURIComponent($j('#fcp_comments_textarea').val());		
		var author	= isLoggedUser ? null : encodeURIComponent($j('#author').val());
		var email	= isLoggedUser ? null : encodeURIComponent($j('#email').val());		
		var emailReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		
		if(author)
			$j("#author").removeClass("textbox-error");
		if(email)
			$j("#email").removeClass("textbox-error");
		if(fcp_comments_textarea)
			$j("#fcp_comments_textarea").removeClass("textbox-error");
		
		if(author == ""){
			$j("#author").addClass("textbox-error");
			return false;
		}
		else if(email != null && (email == "" || emailReg.test(decodeURIComponent(email)) == false)){
			$j("#email").addClass("textbox-error");
			return false;
		}
		else if(fcp_comments_textarea == ""){
			$j("#fcp_comments_textarea").addClass("textbox-error");
			return false;
		}
				
		fcp_comments_textarea = fcp_comments_textarea.replace(/\r\n\r\n/g, "</p><p>");
		fcp_comments_textarea = fcp_comments_textarea.replace(/\r\n/g, "<br />");
		fcp_comments_textarea = fcp_comments_textarea.replace(/\n\n/g, "</p><p>");
		fcp_comments_textarea = fcp_comments_textarea.replace(/\n/g, "<br />");
		
		$j.ajax({	
			type: 'post',
			url:  blogurl+"/wp-content/plugins/facebook-comments-points-fcp/fcp_submit_ajax_comments.php",
			data: $j('#fcpCommentForm').serialize(),
			beforeSend: function(){
				$j('.fcp-buttons').hide();
				$j('.fcp-form-loader').fadeIn();
			},
			success: function(res){
				if ($j(".fcp-comments-no-comment").length > 0)
					$j(".fcp-comments-no-comment").hide();

				$j('#fcp-commment-form-div').parent().before(res);				
				
				$j('.fcp-form-loader').hide();
				$j('.fcp-buttons').fadeIn();
	 			$j('#fcp_comments_textarea').val('');
				$j('#fcp_comments_textarea').focus();
				changeFormPosition();
			},
			error: function(jqXHR, textStatus){
				$j('.fcp-form-loader').hide();
				$j('.fcp-buttons').fadeIn();
	 			$j('#fcp_comments_textarea').val('');
				$j('#fcp_comments_textarea').focus();
				changeFormPosition();
				//alert(jqXHR.responseText);
			}			
		});
	
		return false;
	});	
	
	SetGravatarImage();
	$j("#email").bind("keyup blur", function(event){
		SetGravatarImage();
	});
});

function SetGravatarImage(){
	var emailReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var currentImage = $j("#gravatarContainer h2 img");
	var email = $j.trim($j("#email").val());
	if (emailReg.test(email)){
		var md5Email = MD5(email);
		$j(currentImage).attr("src", "http://www.gravatar.com/avatar/"+md5Email+"?d=mm&s=50");
	}
	else{
		$j(currentImage).attr("src", "http://www.gravatar.com/avatar/00000000000000000000000000000000?d=mm&s=50");
	}
}

function MD5(string){
	var encodedString = "";
	$j.ajax({
		type: "GET",
		async: false,
		url: $j("#hdnTempDirectory").val() + '/ajax-calls.php', // hdnTempDirectory is a hidden field in footer.php
		data: "action=md5&string="+string,
		success: function(data) {
			encodedString = data;
		}
	});
	return encodedString;
}

function fCpLike(comment_ID)
{
	$j('.fcp-like-click-'+comment_ID).hide();
	$j('.fcp-comment-loader-'+comment_ID).show();	
	$j.post( blogurl+"/wp-content/plugins/facebook-comments-points-fcp/fcp_ajax_like.php?id="+comment_ID+"&type=like", {}, function(response){
		$j('.fcp-comment-loader-'+comment_ID).hide();
		$j('.fcp-unlike-click-'+comment_ID).fadeIn();		
		$j(".fcp-likes-stat-"+comment_ID).html(escape(response));
	});
}

function fCpUnLike(comment_ID)
{
	$j('.fcp-unlike-click-'+comment_ID).hide();
	$j('.fcp-comment-loader-'+comment_ID).show();	
	$j.post( blogurl+"/wp-content/plugins/facebook-comments-points-fcp/fcp_ajax_like.php?id="+comment_ID+"&type=unlike", {}, function(response){		
		$j('.fcp-comment-loader-'+comment_ID).hide();
		$j('.fcp-like-click-'+comment_ID).fadeIn();		
		$j(".fcp-likes-stat-"+comment_ID).html(escape(response));		
	});
}

function fcpUserCom(comment_ID,Pid)
{
	$j('#fcp-user-comm-logo-'+comment_ID).hide();
	$j('.fcp-this-user-loader'+comment_ID).show();	
	changeFormPosition();	
	$j.post(ajaxFetchingUrl + "?id="+comment_ID+"&type=userpost&pid="+Pid, {}, 
		function(response){			
			$j(".fcp-comments-box .fcp-comments-inner-container").html(response);
			$j('.fcp-this-user-loader'+comment_ID).hide();
		});
}

function changeFormPosition()
{
	$j('#fcp_comments_textarea').val('');
	$j('#fcp-cancel').hide();
	$j('.fcp-comments-inner-container').after($j('#fcp-commment-form-div').fadeIn());
}

$j('a#fcp_next_rec').live('click', function(){	
	var next = $j('#fcp_paging_id').val();
	var pid = $j('#fcp_paging_pid').val();
	$j('#fcp_next_rec').hide();
	$j('.fcp-paging-loader').show();
	changeFormPosition();	
	$j.post(ajaxFetchingUrl + "?id="+pid+"&type=paging&next="+next, {
		}, function(response){			
			$j(".fcp-comments-box .fcp-paging-wrap-"+next).html(response);
			$j('.fcp-paging-loader').hide();
		});
});